<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    height="calc(100% - 90px)"
    size="default"
    highlight-current-row
  >
    <el-table-column
      label="房间名称"
      prop="name"
      width="200px"
      align="center"
      show-overflow-tooltip
    />
    <el-table-column label="房间图片" align="center">
      <template #default="scope"> <img :src="scope.row.photos[0].url" /></template>
    </el-table-column>
    <el-table-column label="房间价格" prop="price" sortable align="center">
      <template #default="scope"> {{ scope.row.price }}.00 ￥</template>
    </el-table-column>
    <el-table-column label="房间容量" prop="capacity" align="center">
      <template #default="scope"> {{ scope.row.bedroom * 2 }}人</template>
    </el-table-column>
    <el-table-column label="卧室数量" prop="bedroom" align="center">
      <template #default="scope"> {{ scope.row.bedroom }}间</template>
    </el-table-column>
    <el-table-column label="房间服务" align="center" width="230px">
      <template #default="scope">
        <el-tag v-if="scope.row.air" style="margin: 0 5px">空调</el-tag>
        <el-tag v-if="scope.row.wifi" style="margin: 0 5px" type="success">无线网络</el-tag>
        <el-tag v-if="scope.row.washer" style="margin: 0 5px" type="danger">洗衣机</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" prop="updateTime" sortable width="110px" align="center">
      <template #default="scope">{{ scope.row.updateTime.substring(0, 10) }}</template>
    </el-table-column>
    <el-table-column align="right" width="250px">
      <template #header>
        <el-input v-model="search" size="default" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)" type="primary">编辑</el-button>
        <el-button
          size="small"
          @click="handlePutaway(scope.row)"
          type="success"
          v-if="scope.row.soldOut"
          >上架</el-button
        >
        <el-button size="small" @click="handleSoldOut(scope.row)" type="warning" v-else
          >下架</el-button
        >
        <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="next">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="7"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { useRoomStore } from '@/store/roomStore'
import axios from '@/utils/request'
import 'animate.css'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
const roomStore = useRoomStore()
let tableData = ref([])
const search = ''
let count = ref(10)
let router = useRouter()
const init = async () => {
  let countData = await axios.get('/room/count')
  count.value = countData.data
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/room/page',
    params: {
      page: 1,
      take: 7,
    },
  })
  tableData.value = result.data
}
onMounted(() => {
  init()
})
//分页
const handleCurrentChange = async (page: any) => {
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/room/page',
    params: {
      page,
      take: 7,
    },
  })
  tableData.value = result.data
}
//下架房间
const handleSoldOut = async (row: any) => {
  try {
    await axios.patch(`/room/${row.id}`, { soldOut: 1 })
    ElMessage.success('下架成功')
    init()
  } catch (error) {
    ElMessage.error('出错了!下架失败')
  }
}
//上架房间
const handlePutaway = async (row: any) => {
  try {
    await axios.patch(`/room/${row.id}`, { soldOut: 0 })
    ElMessage.success('上架成功')
    init()
  } catch (error) {
    ElMessage.error('出错了!上架失败')
  }
}
// 编辑
const handleEdit = (row: any) => {
  roomStore.currentData = row
  router.push('/home/roomEditor')
}

// 删除操作
const handleDelete = async (row: any) => {
  try {
    await axios.delete(`/room/${row.id}`)
    ElMessage.success('删除成功')
    init()
  } catch (error) {
    ElMessage.error('出错了!删除失败')
  }
}
</script>
<style lang="less" scoped>
img {
  width: 70px;
  height: 70px;
  object-fit: contain;
  border-radius: 8px;
}

.next {
  height: 80px;
  display: flex;
  justify-content: flex-end;
}
</style>
